package com.docMan.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.docMan.dao.BaseDao;
import com.docMan.dao.pojo.DocBookSon;
import com.docMan.service.DocBookSonService;
import com.docMan.utils.FinalConfig;
import com.docMan.utils.FtpUtils;
import com.docMan.utils.OpenOfficeUtil;

@Service("docBookSonService")
public class DocBookSonServiceImpl extends BaseServiceImpl<DocBookSon> implements DocBookSonService {
	private HttpSession session;
	
	@Autowired
	public void setSession(HttpSession session) {
		this.session = session;
	}
	@Resource(name="docBookSonDao")
	@Override
	public void setDao(BaseDao<DocBookSon> dao) {
		// TODO Auto-generated method stub
		super.setDao(dao);
	}
	
	@Override
	public String addBookSon(String bookSonTitle, int fatherBookId,List<String> filePaths) {
		if(fatherBookId!=0){
			for (String filePath : filePaths) {
				DocBookSon docBookSon=new DocBookSon();
				docBookSon.setFatherBookId(fatherBookId);
				docBookSon.setDisplayName(getFileName(filePath));
				docBookSon.setSubTitle(getFileName(filePath).substring(0,getFileName(filePath).lastIndexOf(".")));
				docBookSon.setCreateTime(new Timestamp(System.currentTimeMillis()));
				docBookSon.setOrderId(0);
				save(docBookSon);
				FileInputStream in;
				try {
					in = new FileInputStream(new File(filePath));
					String docBookFtpPath="/book/"+docBookSon.getId();
					FtpUtils.uploadFile(FinalConfig.getDocFtp(), getFileName(filePath), docBookFtpPath, in);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
					putFalse();
					putMessage("上传出现错误，请删除记录后重新上传");
				}
			}
			putSuccess();
		}else {
			putFalse();
		}
		return getMap();
	}
	
	private String getFileName(String path) {
		String fileName = path.substring(path.lastIndexOf("\\") + 1,
				path.length());
		return fileName;
	}

	@Override
	public File getBook(int bookSonId) {
		File file=null;
		if(bookSonId!=0){
			DocBookSon docBookSon=getById(bookSonId);
			//0表示没有上传文件
			if(docBookSon.getIsUploadFile().equals(0))
				return new File("");
			String remotePath="/book/"+docBookSon.getId();
			String filename=docBookSon.getDisplayName();
			
//			String localPath=FinalConfig.getFtpDownloadLocalPath(session.getAttribute(FinalConfig.SESSION_HUMANID));
			Random random1=new Random(10000);
			Random random2=new Random(10000);
			String localPath=FinalConfig
					.getFtpDownloadLocalPath(random1.nextInt(10000)+random2.nextInt(10000));// 这里应该是session中的humanid
			if(FtpUtils.downloadFile(FinalConfig.getDocFtp(), filename, remotePath, localPath)){
				file=new File(localPath+filename);
			}
			if(!filename.endsWith("pdf")){
				String word2pdfFilePaht=localPath+filename.replace(".docx", ".pdf").replace(".doc", ".pdf");
				try {
					OpenOfficeUtil.office2PDF(file.getCanonicalPath(), word2pdfFilePaht);
					file=new File(word2pdfFilePaht);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return file;
	}
	
	@Override
	public String delBookSon(int bookSonId) {
		delete(bookSonId);
		putSuccess();
		return getMap();
	}
	@Override
	public String editBookSon(int bookSonId, String bookSonName,int orderId) {
		DocBookSon docBookSon=getById(bookSonId);
		docBookSon.setSubTitle(bookSonName);
		docBookSon.setOrderId(orderId);
		saveOrUpdate(docBookSon);
		putSuccess();
		return getMap();
	}
	@Override
	public String getSubBookSon(int bookSonId) {
		List<DocBookSon> bookSons=find("select bookSon from DocBookSon bookSon where bookSon.bookSonId=? and bookSon.isDirectory=1 order by bookSon.orderId ", bookSonId);
		put("bookSons", bookSons);
		putSuccess();
		return getMap();
	}
	
	@Override
	public String addBookSonOnly(String bookSonTitle, int bookSonId, int fatherId,int orderId,int isDirectory) {
		DocBookSon docBookSon=new DocBookSon();
		docBookSon.setSubTitle(bookSonTitle);
		if(bookSonId!=0){
			docBookSon.setBookSonId(bookSonId);
		}else{
			docBookSon.setFatherBookId(fatherId);
		}
		docBookSon.setIsDirectory(isDirectory);
		docBookSon.setOrderId(orderId);
		docBookSon.setCreateTime(new Timestamp(System.currentTimeMillis()));
		save(docBookSon);
		put("bookSon", docBookSon);
		return getMap();
	}
	
	@Override
	public String setBook(List<String> filePaths, int bookSonId,int fatherBookId,int isDirectory) {
		FileInputStream in;
		for(String filePath:filePaths){
			try {
				DocBookSon docBookSon=new DocBookSon();
				docBookSon.setBookSonId(bookSonId);
				docBookSon.setDisplayName(getFileName(filePath));
				docBookSon.setSubTitle(getFileName(filePath).substring(0, getFileName(filePath).lastIndexOf(".")));
				docBookSon.setCreateTime(new Timestamp(System.currentTimeMillis()));
				docBookSon.setIsUploadFile(1);
				docBookSon.setFatherBookId(fatherBookId);
				docBookSon.setIsDirectory(isDirectory);
				save(docBookSon);
				in = new FileInputStream(new File(filePath));
				String docBookFtpPath="/book/"+docBookSon.getId();
				docBookSon.setDisplayName(getFileName(filePath));
				if(FtpUtils.uploadFile(FinalConfig.getDocFtp(), getFileName(filePath), docBookFtpPath, in)){
					docBookSon.setIsUploadFile(1);
					saveOrUpdate(docBookSon);
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		putSuccess();
		return getMap();
	}
	@Override
	public String getBookSonByFatherBookId(int fatherId) {
		List<DocBookSon> bookSons=find("select bookSon from DocBookSon bookSon where bookSon.isDirectory=1 and bookSon.fatherBookId=? order by bookSon.orderId ", fatherId);
		put("bookSons", bookSons);
		putSuccess();
		return getMap();
	}
	@Override
	public String getFatherBookSon(int fatherId) {
		List<DocBookSon> bookSons=find("select bookSon from DocBookSon bookSon where bookSon.fatherBookId=? and bookSon.isDirectory=0 order by bookSon.createTime ", fatherId);
		put("bookSons", bookSons);
		putSuccess();
		return getMap();
	}
}
